﻿using Api.Sales.Orders.Messages.Express;
using Api.Sales.Orders.Messages.MessageQueue;
using Api.Sales.Orders.Messages.Payment;
using System;
using System.Dynamic;
using System.Web.Http;

namespace Api.Sales.Orders.Controllers
{
    //[Authorize]
    [RoutePrefix("api/sales/orders")]
    [Route("{action=produce}")]
    public class IndexController : ApiController
    {
        // POST: /api/sales/orders/produce
        [Route("produce")]
        public dynamic Produce([FromBody]dynamic request)
        {
            if (request == null) return NotFound();
            dynamic response = new ExpandoObject();

            // 添加默认的会话信息
            GenerateSessionInfo(request, response);

            // 添加默认的成功结果数据
            GenerateSuccessInfo(response);
            
            try
            {
                // 填充支付单数据（缺失自动获取）
                FillPaymentInfo(request, response);
            }
            catch(Exception ex)
            {
                GeneratePaymentExInfo(response, ex);
                return response;
            }

            try
            {
                // 填充运单数据（缺失自动获取）
                FillExpressInfo(request, response);
            }
            catch (Exception ex)
            {
                GenerateWaybillExInfo(response, ex);
                return response;
            }

            try
            {
                // 发送到消息服务进行申报
                SendToMessageService(request, response);
            }
            catch (Exception ex)
            {
                GenerateMessageServiceExInfo(response, ex);
                return response;
            }

            return response;
        }
        
        void GenerateMessageServiceExInfo(dynamic response, Exception ex)
        {
            response.result = new ExpandoObject();
            response.result.code = "900";
            response.result.message = new ExpandoObject();
            response.result.message.locale = ex.Message;
        }

        void SendToMessageService(dynamic request, dynamic response)
        {
            AliyunMNS.Instance.Send(request, response);
        }

        void GenerateWaybillExInfo(dynamic response, Exception ex)
        {
            response.result = new ExpandoObject();
            response.result.code = "700";
            response.result.message = new ExpandoObject();
            response.result.message.locale = ex.Message;
        }
        
        void FillExpressInfo(dynamic request, dynamic response)
        {
            string expressCode = request.wulqqiyebzojhk;
            if (expressCode == "410198Z062")
                EmsExpress.Instance.GenerateExpressInfo(request, response);
            else
                MeihuaExpress.Instance.GenerateExpressInfo(request, response);
        }

        void GeneratePaymentExInfo(dynamic response, Exception ex)
        {
            response.result = new ExpandoObject();
            response.result.code = "600";
            response.result.message = new ExpandoObject();
            response.result.message.locale = ex.Message;
        }

        void FillPaymentInfo(dynamic request, dynamic response)
        {
            MeihuaPay.Instance.GeneratePaymentInfo(request, response);
        }

        void GenerateSuccessInfo(dynamic response)
        {
            response.result = new ExpandoObject();
            response.result.code = "000";
        }

        void GenerateSessionInfo(dynamic request, dynamic response)
        {
            string trackingNumber = GenerateLongGuid();
            request.tracking_number = trackingNumber;
            response.tracking_number = trackingNumber;
        }

        string GenerateLongGuid()
        {
            byte[] buffer = Guid.NewGuid().ToByteArray();
            return BitConverter.ToInt64(buffer, 0).ToString();
        }
        
    }
}
